package com.platform.qujia.controller;

import com.alibaba.fastjson.JSONObject;
import com.platform.qujia.pojo.Enterprise;
import com.platform.qujia.service.EnterpriseService;
import com.platform.qujia.service.WeChatService;
import com.platform.qujia.utils.StringUtils;
import com.platform.qujia.utils.exception.EndingException;
import io.swagger.annotations.Api;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.stream.Collectors;

@RestController
@Api(tags = "客服对话广告")
public class DialogueAdController {
    private static Logger logger =(Logger) LoggerFactory.getLogger(QuJiaPayV2Controller.class);

    @Resource
    private WeChatService weChatService;

    @Resource
    private EnterpriseService enterpriseService;


    @GetMapping(value = "/dialogueAd")
    public String dialogueAdConfirm(HttpServletRequest request){
        //XTTLeUoJ6sELtATWLcVBjCn1Xxy0Ota87ljD1FoXj4U
        logger.info(request.getQueryString());
        ArrayList<String> list = new ArrayList<String>()
        {
            {
                add("joeley123123");
                add(request.getParameter("timestamp"));
                add(request.getParameter("nonce"));
            }
        };
        Collections.sort(list);
        String decStr = list.stream().collect(Collectors.joining());

        boolean matchFlag = DigestUtils.sha1Hex(decStr).equals(request.getParameter("signature"));

        if (matchFlag)
        {
            return request.getParameter("echostr");
        }
        return null;
    }

    @PostMapping(value = "/dialogueAd")
    public String customerDialogue2(HttpServletRequest request, @RequestBody JSONObject json){
        logger.info(request.getQueryString());
        logger.info(json.toJSONString());

        logger.info("Customer service received a message : " + json.toJSONString());


        String originId = json.getString("ToUserName");
        String openId = json.getString("FromUserName");
        weChatService.customerDialogue(originId, openId);

//
//        Enterprise enterprise = enterpriseService.selectEnterpriseByOriginId(originId);
//        if(enterprise == null || StringUtils.isBlank(enterprise.getAppId()) || StringUtils.isBlank(enterprise.getAppSecret()) ){
//            throw new EndingException("该油企未查到或未配置相关参数");
//        }
//        String dialogueAdId = enterprise.getDialogueAdId();
//
//        if(StringUtils.isBlank(dialogueAdId)){
//            throw new EndingException();
//        }
//
//
//
//        HashMap<String, String> msgMap = new HashMap<>();
//
//        msgMap.put("type","text");
//        msgMap.put("content","https://100000210632.retail.n.weimob.com/saas/retail/100000210632/0/shop/index?id=14061950/&storeId=31868632");
//        msgMap.put("openId","oRwjv4shA0NaGPRLC4aehNJLVNDg");




        return "success";
//        String data = JKPayUtil.getParamByStream(request);
//        JSONObject obj = JSON.parseObject(data);
//        logger.info(obj.toJSONString());
    }

    @GetMapping(value = "/test1")
    public String customerDialogue2(){

        String originId = "gh_b0f9c36e7c48";
        String openId = "oRwjv4shA0NaGPRLC4aehNJLVNDg";
        weChatService.customerDialogue(originId, openId);

        return "success";
    }


}
